<?php
/*
 *      form.php
 *      
 *      Copyright 2011 Vitaliy Rakitin <virak@ukr.net>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
 
 // Попробуем описать здесь идеальное окно
 
require_once("system.php");

class TComponent {
	
	protected $uuid;
	
	function __construct() {
		$_SESSION["components"][$this->uuid] = &$this;
	}
	
	function beforechange() {
		//echo "before $this->uuid<br>";
	}
	
	function sendmessage($receiver, $message, $param1, $param2) {
		$_SESSION["components"][$receiver]->getmessage($this->uuid, $message, $param1, $param2);
	}
	
	function getmessage($sender, $message, $param1, $param2) {
		
	}
	
	function afterchange($name) {
		foreach($_SESSION["hooks"][$this->uuid]["afterchange"] as $key=>$hook) {
			if ($name == $hook[0]) {
				$_SESSION["components"][$key]->{$hook[1]}();
			}
		}
	}
	
	function addhook($uuid, $type, $var, $callback) {
		$_SESSION["hooks"][$uuid][$type][$this->uuid] = array($var, $callback);
	}
}
 
class TFieldEdit extends TComponent {
	
	//protected $object;
	//protected $field;
	protected $value;
	
	function __construct($uuid) {
		$this->uuid = $uuid;
		//$this->uuid = "uuid39e9e7c11f11c3e0a2f0cac4b2b9f87e";
		parent::__construct();
	}
	
	function __get($name) {
		switch ($name) {
			case "uuid":{return $this->uuid;break;}
		}
	}
	
	function __set($name, $value) {
		switch ($name) {
			case "value":{
				$this->beforechange();
				//Some action
				//echo print_r($this->components,1);
				$this->afterchange("value");
				break;
			} 
		}
	}
	
	
}

class TForm  extends TComponent {

	protected $var1;
	protected $fields = array();
	
	function __construct() {
		$this->uuid = "uuid31168153f2b7d26ff2419075b5c8fad8";
		parent::__construct();
		$this->fields[] = new TFieldEdit("uuid39e9e7c11f11c3e0a2f0cac4b2b9f87e");
		$this->addhook($this->fields[0]->uuid,"afterchange","value","callback");
		$this->fields[] = new TFieldEdit("uuid39e9e7c11f11c3e0a2f0cac4b2b9f874");
		//$this->field->value = "sdfsdf";
	}
	
	function test() {
		//echo print_r(get_class_vars(get_class($my_class)),1);
		$this->fields[0]->value = "sfsdf";
	}
	
	function callback() {
		echo "callback $this->uuid<br>";
	}
	
	//include();
	
	//eval();
	//include "message.php";
	
	function __get($name) {
		
	}
	
	function __set($name, $value) {
		switch ($name) {
			case "var1":{
				$this->beforechange();
				//Some action
				//echo print_r($this->components,1);
				$this->afterchange("var1");
				break;
			} 
		}
	}
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>Без имени</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<meta name="generator" content="Geany 0.20" />
</head>

<body>
	
<?php 
session_start();
$a = new TForm();
$a->var1 = "sdff";
$a->test();

echo "<pre>".print_r($_SESSION,1)."</pre>";
//$b->addhook();
?>
	
</body>

</html>
